<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\RecorderTypeModel;
use app\model\UserModel;
use think\Validate;

class User extends Base
{
    protected $model;

    public function __construct(UserModel $userModel)
    {
        parent::__construct();
        $this->model = $userModel;
    }

    /**
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\View
     */
    public function userAdd()
    {
        //代录人类型
        $recorder_type = RecorderTypeModel::select();
        $this->assign('RecorderType',$recorder_type);
        return $this->fetch('user/userAdd');
    }

    /**
     * 修改页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userShow()
    {
        $id = $this->request->param('id','','trim');
        if(!empty($id)){
            $role_arr = [1 => '保险公司人员',2=>'代录人',3=>'被保险人',4=>'业务员',5=>'其他人员'];
            $info = $this->model->where('ui_id',$id)->find();
            $info['role_name'] = $role_arr[$info['role_id']];
            $info['recoder_type'] && $info['recoder_name'] = RecorderTypeModel::where('id',$info['recoder_type'])->value('name');
            $status_arr = [0 => '未激活',1=>'待审核',2=>'审核通过',3=>'审核不通过'];
            $info['status_name'] = $status_arr[$info['status']];
            $this->assign('info',$info);
        }
        return $this->fetch('user/userShow');
    }

    /**
     * 列表接口
     */
    public function getUserInfo()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $arr = ['like'=>'user.company_type','1'=>'user.username'];
        $where = $this->select_param($arr,$param);
        if(isset($param['role_id']) && !empty($param['role_id']) ){
            $where['user.role_id'] = $param['role_id'];
        }else{
            $where['user.role_id'] = 1;
        }
        if(isset($param['username']) && !empty($param['username']) ){
            $where['user.username'] = $param['username'];
        }
        $list = $this->model->getUserListPage($page,$pageSize,$where,false);
        $count = $this->model->getUserListPage(0,0,$where,true);
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }


    /**
     * 添加 修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveUserInfo()
    {
        $param = $this->request->param();
        $rule = [
            //'company_type|单位类型'=>'require|number',
            'role_id|角色类型' => 'require|number',
            'username|员工名称'=>'require',
            'mobile|手机号'=>'require|/^[1][3,4,5,6,,7,8,9][0-9]{9}$/',
            'id_number|身份证号'=>'require|length:18',
            'company|单位名称'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>500,'msg'=>$validate->getError()]);
        }
        $data = [
            'role_id' => $param['role_id'],
            'username'=> $param['username'],
            'mobile'=> $param['mobile'],
            'card_num' => $param['id_number'],
            'company_name'=> $param['company'],
            //'password'=> md5($param['password']),
        ];
        if($param['role_id'] == 2){ //代录人
            $data['recoder_type'] = $param['recoder_type'];
        }
        if($param['role_id'] == 4){ //业务员
            $data['sale_man_company'] = $param['sale_man_company'];
            $data['sale_man_linkman'] = $param['sale_man_linkman'];
        }
        if(!empty($param['id'])){
            $userInfo = $this->model->where('id',$param['id'])->find();
            if($userInfo['password'] == $param['password']){
                $data['password'] = $param['password'];
            }else{
                $data['password'] = md5($param['password']);
            }
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $res = $this->model->where('id',$param['id'])->update($data);
        }else{
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $res = $this->model->insert($data);
        }
        if($res!==false){
            return json(['code'=>200,'msg'=>'更新成功','data'=>$res]);
        }else{
            return json(['code'=>500,'msg'=>'error','data'=>$res]);
        }
    }

    /**
     * 删除用户
     */
    public function delUser()
    {
        $param = $this->request->param();
        $info = $this->model->where('ui_id',$param['id'])->update(['delete_ts'=>time()]);
        if($info){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>500,'msg'=>'删除失败']);
        }
    }

    /**
     * 审核用户
     */
    public function statusUser(){
        if($this->request->isPost()){
            $param = array_filter($this->request->param());
            $old_status = $this->model->where('ui_id',$param['id'])->value('status');
            if(!in_array($param['status'],[0,1,2,3])) return json(['code'=>500,'msg'=>'已是当前状态']);
            if($param['status'] == $old_status) return json(['code'=>500,'msg'=>'已是当前状态']);
            if($this->model->where('ui_id',$param['id'])->setField('status',$param['status'])){
                return json(['code'=>200,'msg'=>'操作成功','data'=>$param]);
            }else{
                return json(['code'=>500,'msg'=>'操作失败']);
            }
        }
    }
}